layui.use(['form', 'layer', 'element', 'laydate', 'table'], function () {
    var form = layui.form;
    var layer = layui.layer;
//寄件人地址
    $.get("/static/json/area.json", function (data) {
        //地址省市区级联
        var province = $("#spro");
        var city = $("#scity");
        var districtSelect = $("#sdis");
        //初始化，将省份数据赋予
        for (var i = 0; i < data.length; i++) {
            addEle(province, data[i].name);
        }
        form.render('select');

        //向select['province']中添加内容
        function addEle(ele, value) {
            var optionStr = "";
            optionStr = '<option value="' + value + '">' + value + '</option>';
            ele.append(optionStr);
        }

        //移除select['province']中所有项，赋予初始值
        function removeEle(ele) {
            ele.find("option").remove();
            var optionStr = "";
            optionStr = '<option value="0">请选择</option>';
            ele.append(optionStr);
        }

        var proText, cityText, cityItem;//被选中省市,市索引
        //选定身份后，添加当前省份数据
        form.on('select(spro)', function (pro) {
            proText = pro.value;
            $.each(data, function (i, item) {
                if (proText == item.name) {
                    cityItem = i;
                    return cityItem;
                }
            });
            removeEle(city);
            removeEle(districtSelect);
            $.each(data[cityItem].city, function (i, item) {
                addEle(city, item.name);
            });
            form.render("select");
        });
        //添加县市区
        form.on('select(scity)', function (c) {
            cityText = c.value;
            $.each(data, function (i, item) {
                if (cityText == item.name) {
                    cityItem = i;
                    return cityItem;
                }
            });
            removeEle(districtSelect);
            $.each(data[cityItem].city, function (i, item) {
                if (cityText == item.name) {
                    for (var j = 0; j < item.district.length; j++) {
                        addEle(districtSelect, item.district[j]);
                    }
                }
                form.render("select");
            });
        });
    });

//取件地址
    $.get("/static/json/area.json", function (data) {
        //地址省市区级联
        var province = $("#qpro");
        var city = $("#qcity");
        var districtSelect = $("#qdis");
        //初始化，将省份数据赋予
        for (var i = 0; i < data.length; i++) {
            addEle(province, data[i].name);
        }
        form.render('select');

        //向select['province']中添加内容
        function addEle(ele, value) {
            var optionStr = "";
            optionStr = '<option value="' + value + '">' + value + '</option>';
            ele.append(optionStr);
        }

        //移除select['province']中所有项，赋予初始值
        function removeEle(ele) {
            ele.find("option").remove();
            var optionStr = "";
            optionStr = '<option value="0">请选择</option>';
            ele.append(optionStr);
        }

        var proText, cityText, cityItem;//被选中省市,市索引
        //选定身份后，添加当前省份数据
        form.on('select(qpro)', function (pro) {
            proText = pro.value;
            $.each(data, function (i, item) {
                if (proText == item.name) {
                    cityItem = i;
                    return cityItem;
                }
            });
            removeEle(city);
            removeEle(districtSelect);
            $.each(data[cityItem].city, function (i, item) {
                addEle(city, item.name);
            });
            form.render("select");
        });
        //添加县市区
        form.on('select(qcity)', function (c) {
            cityText = c.value;
            $.each(data, function (i, item) {
                if (cityText == item.name) {
                    cityItem = i;
                    return cityItem;
                }
            });
            removeEle(districtSelect);
            $.each(data[cityItem].city, function (i, item) {
                if (cityText == item.name) {
                    for (var j = 0; j < item.district.length; j++) {
                        addEle(districtSelect, item.district[j]);
                    }
                }
                form.render("select");
            });
        });
    });

//收件人地址
    $.get("/static/json/area.json", function (data) {
        //地址省市区级联
        var province = $("#rpro");
        var city = $("#rcity");
        var districtSelect = $("#rdis");
        //初始化，将省份数据赋予
        for (var i = 0; i < data.length; i++) {
            addEle(province, data[i].name);
        }
        form.render('select');

        //向select['province']中添加内容
        function addEle(ele, value) {
            var optionStr = "";
            optionStr = '<option value="' + value + '">' + value + '</option>';
            ele.append(optionStr);
        }

        //移除select['province']中所有项，赋予初始值
        function removeEle(ele) {
            ele.find("option").remove();
            var optionStr = "";
            optionStr = '<option value="0">请选择</option>';
            ele.append(optionStr);
        }

        var proText, cityText, cityItem;//被选中省市,市索引
        //选定身份后，添加当前省份数据
        form.on('select(rpro)', function (pro) {
            proText = pro.value;
            $.each(data, function (i, item) {
                if (proText == item.name) {
                    cityItem = i;
                    return cityItem;
                }
            });
            removeEle(city);
            removeEle(districtSelect);
            $.each(data[cityItem].city, function (i, item) {
                addEle(city, item.name);
            });
            form.render("select");
        });
        //添加县市区
        form.on('select(rcity)', function (c) {
            cityText = c.value;
            $.each(data, function (i, item) {
                if (cityText == item.name) {
                    cityItem = i;
                    return cityItem;
                }
            });
            removeEle(districtSelect);
            $.each(data[cityItem].city, function (i, item) {
                if (cityText == item.name) {
                    for (var j = 0; j < item.district.length; j++) {
                        addEle(districtSelect, item.district[j]);
                    }
                }
                form.render("select");
            });
        });
    });
});